// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.text.templating;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import se.krka.kahlua.j2se.KahluaTableImpl;
import zombie.debug.DebugLog;

public class ReplaceProvider implements IReplaceProvider {
    protected final Map<String, IReplace> m_keys = new HashMap();

    public void addKey(String key, final String value) {
        this.addReplacer(key, new IReplace() {
            @Override
            public String getString() {
                return value;
            }
        });
    }

    public void addKey(String string, KahluaTableImpl kahluaTableImpl) {
        try {
            ArrayList arrayList = new ArrayList();

            for (int _int = 1; _int < kahluaTableImpl.len() + 1; _int++) {
                arrayList.add((String)kahluaTableImpl.rawget(_int));
            }

            if (arrayList.size() > 0) {
                this.addReplacer(string, new ReplaceList(arrayList));
            } else {
                DebugLog.log("ReplaceProvider -> key '" + string + "' contains no entries, ignoring.");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    public void addReplacer(String key, IReplace replace) {
        if (this.m_keys.containsKey(key.toLowerCase())) {
            DebugLog.log("ReplaceProvider -> Warning: key '" + key + "' replaces an existing key.");
        }

        this.m_keys.put(key.toLowerCase(), replace);
    }

    @Override
    public boolean hasReplacer(String key) {
        return this.m_keys.containsKey(key);
    }

    @Override
    public IReplace getReplacer(String key) {
        return (IReplace)this.m_keys.get(key);
    }
}
